/**
 * 登录模块，提供登录相关业务功能
 * 
 * @Date: 2015-6-16
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "datagridServ","msgServ", "ajaxServ", "formServ", "bizProto"],
		function($,datagridServ, msgServ, ajaxServ, formServ, bizProto) {

			'use strict';

			var _bizProto,
				//锚记datagrid目标元素
				$dg,
				//初始化后的datagrid dom对象
				$grid,
				//编辑弹窗对象及弹窗按钮对象
				$edit_window,window_btn,
				//设置密码弹窗对象及按钮对象
				$password_window,password_btn,
				//业务表单对象
				$form,
				//部门选择框字段
				$deptNameField, $deptIdField,formEl, formData = 0, 
				baseMainPageUrl='/duties/leaveMainPage.action',mainPageUrl,				
				baseDelUrl = '/duties/leaveDel.action',userDelUrl, 
				baseSaveUrl = '/duties/leaveSave.action', userSaveUrl,				
				baseQueryUrl = "/duties/leaveListDomain.action",userQueryUrl
				

			var public_method = {
				
				
				init:function(doc){
					private_method.init(doc);
				}
				
			}

			var private_method = {
				init : function(doc) {
					console.info('user modules gets ready and been loaded.');
					
					_bizProto = new bizProto(doc,'panel-user','div.well')
					private_method.initPath();
					private_method.initElement(_bizProto.pageDoc)
				},
				
				//初始化路径
				initPath:function(){
					userDelUrl = _bizProto.rootPath + baseDelUrl;
					userQueryUrl = _bizProto.rootPath + baseQueryUrl;
					userSaveUrl = _bizProto.rootPath +baseSaveUrl;
					passwordSaveUrl = _bizProto.rootPath +basePasswordUrl;
					deptQueryUrl = _bizProto.rootPath +baseDeptQueryUrl;
					editPageUrl = _bizProto.rootPath +baseEditPageUrl;
					passwordPageUrl = _bizProto.rootPath +basePasswordPageUrl;
				},
				
				
				//初始化元素
				initElement:function(doc){
					
					
					$dg = $("#user-dg");
					$grid = $dg.datagrid({
						height:_bizProto.pageHeight,
						url : userQueryUrl,
						width : 'auto',
						pagination : true,
						rownumbers : true,
						fitcolumns : true,
						border : true,
						singleSelect : true,
						striped : true,
						columns : [[
							{field : 'ck',checkbox : true}, 
							{field : 'userCode',title : '用户编码',width : _bizProto.pageWidth * 0.1,align : 'left',editor : "text"}, 
							{field : 'loginName',title : '用户账号',width : _bizProto.pageWidth * 0.1,align : 'left',editor : "text"}, 
							{field : 'name',title : '用户名',width : _bizProto.pageWidth * 0.1,editor : 
								{type : 'validatebox',options : 
									{required : true}
								}
							},
							{field : 'email',title : '电子邮箱',width : _bizProto.pageWidth * 0.15,align : 'left',editor : 
								{type : 'validatebox',options : 
									{required : true,validType : 'email'}
								}
							},
							{field : 'workphone',title : '办公电话',width : _bizProto.pageWidth * 0.15,editor : "validatebox"}, 							 
							{field : 'mobile',title : '联系电话',width : _bizProto.pageWidth * 0.15,align : 'left',editor : "text"}, 
							
							{field : 'deptName',title : '组织部门',width : _bizProto.pageWidth * 0.1,align : 'left',editor : "text"}, 
							{field : 'comments',title : '描述',width : _bizProto.pageWidth * 0.1,align : 'left',editor : "text"}
						]],
						toolbar : '#tb-user'
					});
					
					

					// 绑定按钮事件
					var btn = $('#tb-user a.function-btn')
					$(btn).unbind('click');
					$(btn).bind('click', function(e) {
								private_method.function_btn_listenner(e)
							})

					// 编辑窗口初始化
					$edit_window = $(doc).find('#user-win')
					window_btn = $($edit_window).find('a.edit-window-btn')
					$(window_btn).unbind('click')
					$(window_btn).bind('click',	private_method.function_btn_listenner)
					$form = $($edit_window).find('form')
					
					//密码窗口初始化
					$password_window = $(doc).find('#userpassword-win');
					password_btn = $($password_window).find('a.easyui-linkbutton')
					$(password_btn).unbind('click')
					$(password_btn).bind('click',	private_method.function_btn_listenner)
					
					//初始化部门选择框
					$deptNameField = $($edit_window).find("#deptName")
					$deptIdField = $($form).find("#deptid")
					$($deptIdField).combotree({
						width:171,
						url:deptQueryUrl,
						idFiled:'id',
					 	textFiled:'name',
					 	parentField:'parentid',
					 	onClick:function(node){private_method.setComboValue(node,$deptNameField)}					 	
					});
					// 搜索框
					$("#searchbox").searchbox({
						menu : "#mm",
						prompt : '模糊查询',
						searcher : function(value, name) {
							var obj = {};
							obj["searchName"]=name;
							obj["searchValue"]=value;							
							$dg.datagrid('reload', obj);
						}

					});

				},
				
				setComboValue:function(node,target){
		    		if(node!=null){		    			
		    			$(target).val(node.text);
		    		}
		    	},
				// 功能按钮监听函数
				function_btn_listenner : function(e) {

					var _text;
					if (e != null) {
						e.preventDefault();
						_text = e.currentTarget.text

						if (_text != null) {
							switch (_text.trim()) {
								case "新增" :
									private_method.add();
									break;
								case "编辑" :
									private_method.edit(true);
									break;
								case "删除" :
									private_method.del();
									break;	
								case "设置密码" :
									private_method.setPassword();
									break;
								
							}
						}

					}

				},
				// 新增函数
				add : function() {
					$form = $($edit_window).find('form')
					$($form).form('clear');
					$edit_window.window('open');

				},
				// 编辑,flag 为form按钮可见性标志
				edit : function(flag) {
					var _param = {grid:$grid,window:$edit_window,href:editPageUrl,width:600,height:400}
					datagridServ.windowEdit(_param);
				},
				
				// 设置密码,flag 为form按钮可见性标志
				setPassword : function(flag) {
					var _param = {grid:$grid,window:$password_window,href:passwordPageUrl,width:400,height:300}
					datagridServ.windowEdit(_param);
				},
				
				// 删除
				del : function() {
					console.info('department delete on air ...');
					datagridServ.delRow({url:userDelUrl,target:$grid});
				},
				// 提交form
				submit : function() {
					console.info('department submit on air ...');
					datagridServ.save({form:$form,grid:$grid,url:userSaveUrl})					
				},
				
				
				//
				cancel : function() {
					$($edit_window).window('close');
				},
				updCellTree:function (record){
				/*var rows = $dg.datagrid('getSelections');
				$.each(rows,function(i,row){
					if (row) {
						var rowIndex = $dg.datagrid('getRowIndex', row);
						$dg.datagrid('getEditor',{'index':rowIndex,'field':'organizeName'}).target.val(record.text);
					}
				});*/
					var rowIdx = $grid.datagrid('getRowIndex',$grid.datagrid('getSelected'));
					 $grid.datagrid('getEditor',{'index':rowIdx,'field':'organizeName'}).target.val(record.text);
	
				}

			};

			
			return public_method;

		});